home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 August / macformat-027.iso / mac / Shareware City / Developers / PlayerPRO 4.4.1 Dev.Kit / Import⁄Export / APPL.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-07  |  10.2 KB  |  441 lines  |  [TEXT/MPCC]

  1. /********************                        ***********************/
  2. //
  3. //    Player PRO 4.4x -- APPL to MADx & MADx to APPL
  4. //
  5. //    Version 1.0    - 12.3.95 ANR
  6. //
  7. //    To use with CodeWarrior 68K or PPC
  8. //
  9. //    Antoine ROSSET
  10. //    16 Tranchees
  11. //    1206 GENEVA
  12. //    SWITZERLAND
  13. //    
  14. //    FAX:            (+41 22) 346 11 97
  15. //    Compuserve:        100277,164
  16. //    Internet:         rosset@dial.eunet.ch
  17. //
  18. /********************                        ***********************/
  19.  
  20. #include "MOD.h"
  21. #include "MAD.h"
  22. #include "RDriver.h"
  23. #include "PPInOut.h"
  24.  
  25. #if defined(powerc) || defined(__powerc)
  26. enum {
  27.         PlayerPROPlug = kCStackBased
  28.         | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
  29.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( OSType)))
  30.         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( Ptr*)))
  31.         | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof( MADPartition*)))
  32.         | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( PPInfoRec*)))
  33.         | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof( short**)))
  34. };
  35.  
  36. ProcInfoType __procinfo = PlayerPROPlug;
  37. #else
  38. #include <A4Stuff.h>
  39. #endif
  40.  
  41. void pStrcpy(register unsigned char *s1, register unsigned char *s2)
  42. {
  43.     register short len, i;
  44.     
  45.     len = *s2;
  46.     for ( i = 0; i <= len; i++) s1[ i] = s2[ i];
  47. }
  48.  
  49. OSErr LoadMADF( Ptr MADPtr, MADPartition *MadFile)
  50. {
  51. short                     i, maxInstru;
  52. long                     inOutCount, OffSetToSample;
  53. struct PatHeader        tempPatHeader;
  54. MADSpec                    *MadHeader;
  55.  
  56. /**** HEADER ****/
  57. MadFile->header = (MADSpec*) NewPtr( sizeof( MADSpec));
  58. if( MadFile->header == 0L) return needMoreMemory;
  59.  
  60. OffSetToSample = 0L;
  61. BlockMove( MADPtr, MadFile->header, sizeof( MADSpec));
  62. OffSetToSample += sizeof( MADSpec);
  63.  
  64. MadHeader = MadFile->header;
  65.  
  66. if( MadHeader->MADIdentification != 'MADG') return fileNotSupportedByThisPlug;
  67.  
  68. /**** PARTITION ****/
  69. for( i = MadHeader->PatMax; i < MAXPATTERN; i++) MadFile->partition[ i] = 0L;
  70. for( i = 0; i < MadHeader->PatMax; i++)
  71. {
  72.     inOutCount = sizeof( struct PatHeader);
  73.     BlockMove( MADPtr + OffSetToSample, &tempPatHeader, inOutCount);
  74.  
  75.     inOutCount = sizeof( struct MusicPattern) + MadHeader->Tracks * tempPatHeader.PatternSize * sizeof( struct Command);
  76.     MadFile->partition[ i] = (struct MusicPattern*) NewPtr( inOutCount);
  77.     if( MadFile->partition[ i] == 0L) return needMoreMemory;
  78.  
  79.     BlockMove( MADPtr + OffSetToSample, MadFile->partition[ i], inOutCount);
  80.     OffSetToSample += inOutCount;
  81. }
  82.  
  83. /**** INSTRUMENTS ****/
  84. maxInstru = MAXINSTRU;
  85. for( i = 0; i < maxInstru ; i++)
  86. {
  87.     if( MadHeader->fid[i].insSize > 0)
  88.     {
  89.         inOutCount = MadHeader->fid[i].insSize;
  90.  
  91.         MadFile->instrument[i] = NewPtr( inOutCount);
  92.         if( MadFile->instrument[i] == 0L) return needMoreMemory;
  93.  
  94.         BlockMove( MADPtr + OffSetToSample, MadFile->instrument[ i], inOutCount);
  95.         OffSetToSample += inOutCount;
  96.     }
  97.     else MadFile->instrument[i] = 0L;
  98. }
  99. /*********************/
  100. return( noErr);
  101. }
  102.  
  103. OSErr TESTMADF( MADSpec* MADPtr)
  104. {
  105.     if( MADPtr->MADIdentification == 'MADG') return noErr;
  106.     else return fileNotSupportedByThisPlug;
  107. }
  108.  
  109. OSErr INFOMADF( MADSpec* MADPtr, PPInfoRec *info)
  110. {
  111. short    i;
  112.  
  113.     CtoPstr( MADPtr->NameSignature);
  114.     pStrcpy( info->internalFileName, (unsigned char*) MADPtr->NameSignature);
  115.     PtoCstr( (unsigned char*) MADPtr->NameSignature);
  116.     
  117.     pStrcpy( info->formatDescription, "\pMADG Resource (APPL)");
  118.     
  119.     info->totalPatterns        = MADPtr->PatMax;
  120.     info->partitionLength    = MADPtr->numPointers;
  121.     
  122.     info->totalTracks        = MADPtr->Tracks;
  123.     info->signature            = MADPtr->MADIdentification;
  124.     
  125.     info->totalInstruments    = 0;
  126.     
  127.     for( i = 0; i < MAXINSTRU ; i++)
  128.     {
  129.         if( MADPtr->fid[i].insSize > 0) info->totalInstruments++;
  130.     }
  131.  
  132.     return noErr;
  133. }
  134.  
  135. void TurnRadio( short    item, DialogPtr    dlog, Boolean alors)
  136. {
  137. Handle        itemHandle;
  138. short        itemType;
  139. Rect        itemRect;
  140.  
  141. GetDItem (dlog, item, &itemType, &itemHandle, &itemRect);
  142.  
  143. if( alors) SetCtlValue( (ControlHandle) itemHandle, 255);
  144. else SetCtlValue( (ControlHandle) itemHandle, 0);
  145. }
  146.  
  147. short ChooseCompilation()
  148. {
  149. short        itemHit, result;
  150. DialogPtr    aDialog;
  151.  
  152.     aDialog = GetNewDialog( 173, 0L, (WindowPtr) -1L);
  153.     SetPort( aDialog);
  154.     
  155.     SetDialogDefaultItem( aDialog, 1);
  156.     
  157.     ShowWindow( aDialog);    
  158.  
  159.     TurnRadio( 6, aDialog, true);
  160.     result = 6;
  161.     
  162.     do
  163.     {
  164.         ModalDialog( 0L, &itemHit);
  165.         
  166.         switch( itemHit)
  167.         {
  168.             case 4:
  169.             case 5:
  170.             case 6:
  171.                 TurnRadio( result, aDialog, false);
  172.                 TurnRadio( itemHit, aDialog, true);
  173.                 result = itemHit;
  174.             break;
  175.         }
  176.     
  177.     }while( itemHit != 1 && itemHit != 2);
  178.     
  179.     DisposDialog( aDialog);
  180.     
  181.     if( itemHit == 2) return -1;
  182.     
  183.     result -= 4;
  184.     return result;
  185. }
  186.  
  187. void CopyResource( OSType type, short ID, short newID)
  188. {
  189. Handle    hRsrc;
  190.  
  191.     hRsrc = GetResource( type, ID);            DetachResource( hRsrc);
  192.     AddResource( hRsrc, type, newID, "\p");    WriteResource( hRsrc);
  193.     DetachResource( hRsrc);                    DisposHandle( hRsrc);
  194. }
  195.  
  196. OSErr SaveMODAPPL( short APPLType, short fRefNum, MADPartition *MadFile)
  197. {
  198. OSErr                    iErr;
  199. short                    i;
  200. long                    fileSize, inOutCount, tt;
  201. Handle                    hRsrc;
  202.  
  203.     fileSize = sizeof( MADSpec);
  204.     for( i = 0; i < MadFile->header->PatMax; i++) fileSize += sizeof( struct PatHeader) + MadFile->header->Tracks * MadFile->partition[ i]->header.PatternSize * sizeof( struct Command);
  205.     for( i = 0; i < MAXINSTRU ; i++) fileSize += MadFile->header->fid[i].insSize;
  206.  
  207.     hRsrc = NewHandle( fileSize);
  208.     if( hRsrc == 0L) return needMoreMemory;
  209.     
  210.     tt = 0L;
  211.     HLock( hRsrc);
  212.     
  213.     inOutCount = GetPtrSize( (Ptr) MadFile->header);
  214.     BlockMove( MadFile->header, (*hRsrc), inOutCount);
  215.     tt += inOutCount;
  216.     
  217.     for( i = 0; i < MadFile->header->PatMax ; i++)
  218.     {
  219.         MadFile->partition[ i]->header.CompressionMode = 'NONE';
  220.         inOutCount = sizeof( struct PatHeader) + MadFile->header->Tracks * MadFile->partition[ i]->header.PatternSize * sizeof( struct Command);
  221.         BlockMove( (Ptr) MadFile->partition[ i], (*hRsrc) + tt, inOutCount);
  222.         tt += inOutCount;
  223.     }
  224.  
  225.     for( i = 0; i < MAXINSTRU ; i++)
  226.     {
  227.         inOutCount = MadFile->header->fid[i].insSize;
  228.         if( inOutCount > 0)
  229.         {
  230.             if( (*hRsrc) + inOutCount > (*hRsrc) + fileSize) DebugStr("\pSaveMOD ERROR");
  231.             
  232.             BlockMove( MadFile->instrument[ i], (*hRsrc) + tt, inOutCount);
  233.             tt += inOutCount;
  234.         }
  235.     }
  236.     
  237.     HUnlock( hRsrc);
  238.     
  239.     SetHandleSize( hRsrc, tt);
  240.     
  241.     AddResource( hRsrc, 'MADF', 3214, "\p");
  242.     WriteResource( hRsrc);
  243.     DetachResource( hRsrc);
  244.     DisposHandle( hRsrc);
  245.     
  246.     /**** On copie les ressources nécessaires pour l'application **/
  247.  
  248.     if( APPLType == 0 || APPLType == 2)
  249.     {
  250.         CopyResource( 'CODE', 100, 0);
  251.         CopyResource( 'CODE', 101, 1);
  252.         CopyResource( 'CODE', 102, 2);
  253.         CopyResource( 'CODE', 103, 3);
  254.         CopyResource( 'DREL', 100, 0);
  255.         CopyResource( 'ZERO', 100, 0);
  256.         CopyResource( 'DATA', 100, 0);
  257.     }
  258.  
  259.     CopyResource( 'BGGB', 128, 128);        // In PlayerPRO !!
  260.     CopyResource( 'ALRT', 3215, 3215);
  261.     CopyResource( 'DITL', 3215, 3215);
  262.     CopyResource( 'DITL', 3214, 3214);
  263.     CopyResource( 'DLOG', 3214, 3214);
  264.     CopyResource( 'dctb', 3214, 3214);
  265.     CopyResource( 'Prou', 0, 0);
  266.     CopyResource( 'FREF', 228, 128);
  267.     CopyResource( 'FREF', 229, 129);
  268.     CopyResource( 'FREF', 230, 130);
  269.     CopyResource( 'BNDL', 228, 128);
  270.     CopyResource( 'WDEF', 205, 200);
  271.     CopyResource( 'PICT', 140, 140);
  272.     CopyResource( 'MENU', 228, 128);
  273.     CopyResource( 'MDEF', 260, 260);
  274.     
  275.     
  276.     CopyResource( 'icl8', 228, 128);
  277.     CopyResource( 'ICN#', 228, 128);
  278.     CopyResource( 'ics#', 128, 128);
  279.  
  280.     hRsrc = GetResource( 'SIZE', 99);        DetachResource( hRsrc);
  281.     AddResource( hRsrc, 'SIZE', -1, "\p");
  282.  
  283.     fileSize = 400L * 1024L;
  284.     *((long*)((*hRsrc)+2)) = (long) fileSize;
  285.     *((long*)((*hRsrc)+6)) = (long) fileSize;
  286.     
  287.     WriteResource( hRsrc);                    DetachResource( hRsrc);        DisposHandle( hRsrc);
  288.  
  289.     if( APPLType == 1 || APPLType == 2)
  290.     {
  291.         hRsrc = GetResource( 'XXXX', 128);        
  292.         inOutCount = SizeResource( hRsrc);
  293.         DetachResource( hRsrc);
  294.         HLock( hRsrc);
  295.         iErr = FSWrite(fRefNum, &inOutCount, *hRsrc);
  296.         HUnlock( hRsrc);
  297.         DisposHandle( hRsrc);
  298.  
  299.         CopyResource( 'cfrg', 100, 0);
  300.     }
  301.     
  302.     return noErr;
  303. }
  304.  
  305. /*****************/
  306. /* MAIN FUNCTION */
  307. /*****************/
  308.  
  309. OSErr main( OSType order, FSSpec *AlienFileFSSpec, MADPartition *MadFile, PPInfoRec *info, short *MADpitchTable)
  310. {
  311.     OSErr    myErr;
  312.     short    vRefNum, iFileRefI;
  313.     long    dirID;
  314.     Handle    myRes;
  315.     
  316. #ifndef powerc
  317.     long    oldA4 = SetCurrentA4();             //this call is necessary for strings in 68k code resources
  318. #endif
  319.  
  320.     HGetVol( 0L, &vRefNum, &dirID);
  321.     HSetVol( 0L, AlienFileFSSpec->vRefNum, AlienFileFSSpec->parID);
  322.  
  323.     myErr = noErr;
  324.  
  325.     switch( order)
  326.     {
  327.         case 'IMPL':
  328.             iFileRefI = OpenResFile( AlienFileFSSpec->name);
  329.             if( iFileRefI == -1) myErr = unknowError;
  330.             else
  331.             {
  332.                 UseResFile( iFileRefI);
  333.                 
  334.                 if( Count1Resources( 'MADF') > 0)
  335.                 {
  336.                     myRes = Get1IndResource( 'MADF', 1);
  337.                     DetachResource( myRes);
  338.                     HLock( myRes);
  339.                     
  340.                     myErr = LoadMADF( *myRes, MadFile);
  341.                     
  342.                     HUnlock( myRes);
  343.                     DisposHandle( myRes);
  344.                 }
  345.                 else myErr = unknowError;
  346.                 
  347.                 CloseResFile( iFileRefI);
  348.             }
  349.         break;
  350.         
  351.         case 'TEST':
  352.             iFileRefI = OpenResFile( AlienFileFSSpec->name);
  353.             if( iFileRefI == -1) myErr = unknowError;
  354.             else
  355.             {
  356.                 UseResFile( iFileRefI);
  357.                 
  358.                 if( Count1Resources( 'MADF') > 0)
  359.                 {
  360.                     myRes = Get1IndResource( 'MADF', 1);
  361.                     DetachResource( myRes);
  362.                     HLock( myRes);
  363.                     
  364.                     myErr = TESTMADF( (MADSpec*) *myRes);
  365.                     
  366.                     HUnlock( myRes);
  367.                     DisposHandle( myRes);
  368.                 }
  369.                 else myErr = unknowError;
  370.                 
  371.                 CloseResFile( iFileRefI);
  372.             }
  373.         break;
  374.         
  375.         case 'EXPL':
  376.             {
  377.                 short    APPLType;
  378.                 short    fRefNum;
  379.                 
  380.                 APPLType = ChooseCompilation();
  381.                 if( APPLType == -1) break;
  382.                 
  383.                 FSDelete( AlienFileFSSpec->name, 0);
  384.                 Create( AlienFileFSSpec->name, 0, 'Prou', 'APPL');
  385.                 myErr = FSOpen( AlienFileFSSpec->name, 0, &fRefNum);
  386.                 if( myErr == noErr)
  387.                 {
  388.                     CreateResFile( AlienFileFSSpec->name);
  389.                     iFileRefI = OpenResFile( AlienFileFSSpec->name);
  390.                     if( iFileRefI == -1) myErr = unknowError;
  391.                     else
  392.                     {
  393.                         UseResFile( iFileRefI);
  394.                         
  395.                         myErr = SaveMODAPPL( APPLType, fRefNum, MadFile);
  396.                         
  397.                         CloseResFile( iFileRefI);
  398.                     }
  399.                     FSClose( fRefNum);
  400.                 }
  401.             }
  402.         break;
  403.  
  404.         case 'INFO':
  405.             iFileRefI = OpenResFile( AlienFileFSSpec->name);
  406.             if( iFileRefI == -1) myErr = unknowError;
  407.             else
  408.             {
  409.                 UseResFile( iFileRefI);
  410.                 
  411.                 if( Count1Resources( 'MADF') > 0)
  412.                 {
  413.                     myRes = Get1IndResource( 'MADF', 1);
  414.                     info->fileSize = SizeResource( myRes);
  415.                     
  416.                     DetachResource( myRes);
  417.                     HLock( myRes);
  418.                     
  419.                     myErr = INFOMADF( (MADSpec*) *myRes, info);
  420.                     
  421.                     HUnlock( myRes);
  422.                     DisposHandle( myRes);
  423.                 }
  424.                 else myErr = unknowError;
  425.                 
  426.                 CloseResFile( iFileRefI);
  427.             }
  428.         break;
  429.         
  430.         default:
  431.             myErr = orderNotImplemented;
  432.         break;
  433.     }
  434.  
  435.     HSetVol( 0L, vRefNum, dirID);
  436.  
  437.     #ifndef powerc
  438.         SetA4( oldA4);
  439.     #endif
  440.     return myErr;
  441. }